package xinyu.customer.news;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.logos.common.widget.display.DisplayHelper;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import xinyu.customer.R;
import xinyu.customer.base.AppBaseFragment;
import xinyu.customer.base.BaseFragmentAdapter;
import xinyu.customer.widget.ScaleTransitionPagerTitleView;

/**
 * description ： TODO:类的作用
 * author : SIGHT-WXC
 * email : 391335693@qq.com
 * date : 2020-04-14 10:55
 */
public class NewsFragment extends AppBaseFragment {

    ViewPager mViewPager;
    RelativeLayout rlTitle;
    MagicIndicator mMagicIndicator;

    private ArrayList<Fragment> fragments;
    private BaseFragmentAdapter adapter;

    private static final String[] CHANNELS = new String[]{"消息", "通讯录"};
    private List<String> mDataList = Arrays.asList(CHANNELS);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_news, null);
        initView(view);
        initVp();
        initMagicIndicator();
        return view;
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(24);
                simplePagerTitleView.setPadding(DisplayHelper.dpToPx(5),0,DisplayHelper.dpToPx(5),0);
                simplePagerTitleView.setNormalColor(getContext().getResources().getColor(R.color.bottom_unselect_color));
                simplePagerTitleView.setSelectedColor(getContext().getResources().getColor(R.color.describe_color_3));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 4));
                indicator.setLineWidth(UIUtil.dip2px(context, 15));
                indicator.setRoundRadius(UIUtil.dip2px(context, 2));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.5f));
                indicator.setColors(Color.parseColor("#FF6CB2"));
                return indicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }


    private void initView(View view) {
        rlTitle = view.findViewById(R.id.rl_title);
        mViewPager = view.findViewById(R.id.vp);
        mMagicIndicator = view.findViewById(R.id.magic_indicator);
        measureTitleBarHeight(rlTitle);

    }

    private void initVp() {
        fragments = new ArrayList<>();
        AppBaseFragment newsTabMainFragment = new NewsTabMainFragment();
        AppBaseFragment newsTabNoteFragment = new NewsTabNoteFragment();
        fragments.add(newsTabMainFragment);
        fragments.add(newsTabNoteFragment);

        adapter = new BaseFragmentAdapter(getChildFragmentManager(), fragments);
        mViewPager.setAdapter(adapter);
    }


}
